package fiveagon.lsystem;

import java.util.HashMap;
import java.util.Map;

public class SimpleGrammar extends AbstractGrammar {
  private final Map<Character, String> ruleMap = new HashMap<>();

  public SimpleGrammar(String axiom) {
    super(axiom);
  }

  public void addRule(char c, String s) {
    ruleMap.put(Character.valueOf(c), s);
  }

  @Override
  String generate(String s) {
    StringBuilder stringBuilder = new StringBuilder();
    for (int i = 0, n = s.length(); i < n; i++) {
      char c = s.charAt(i);
      Character key = Character.valueOf(c);
      if (ruleMap.containsKey(key)) {
        stringBuilder.append(ruleMap.get(key));
      } else {
        stringBuilder.append(c);
      }
    }
    return stringBuilder.toString();
  }
}
